package com.qixin.modules.eaApplication.service.impl;

import com.qixin.modules.eaApplication.entity.EaBusinessApply;
import com.qixin.modules.eaApplication.entity.EaBusinessPerDtl;
import com.qixin.modules.eaApplication.mapper.EaBusinessPerDtlMapper;
import com.qixin.modules.eaApplication.mapper.EaBusinessApplyMapper;
import com.qixin.modules.eaApplication.service.IEaBusinessApplyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: ea_business_apply
 * @Author: qixin-boot
 * @Date:   2022-01-21
 * @Version: V1.0
 */
@Service
public class EaBusinessApplyServiceImpl extends ServiceImpl<EaBusinessApplyMapper, EaBusinessApply> implements IEaBusinessApplyService {

	@Autowired
	private EaBusinessApplyMapper eaBusinessApplyMapper;
	@Autowired
	private EaBusinessPerDtlMapper eaBusinessPerDtlMapper;
	
	@Override
	@Transactional
	public void saveMain(EaBusinessApply eaBusinessApply, List<EaBusinessPerDtl> eaBusinessPerDtlList) {
		eaBusinessApplyMapper.insert(eaBusinessApply);
		if(eaBusinessPerDtlList!=null && eaBusinessPerDtlList.size()>0) {
			for(EaBusinessPerDtl entity:eaBusinessPerDtlList) {
				//外键设置
				entity.setEaBusinessApplyId(eaBusinessApply.getId());
				eaBusinessPerDtlMapper.insert(entity);
			}
		}
		// update-begin-Autor: wmf date: 2022-02-08 12:51 for: 把原单据修改为已变更
		if (StringUtils.isNotBlank(eaBusinessApply.getParentId())){
			EaBusinessApply apply = new EaBusinessApply();
			apply.setIfChanged(1); // 改为已变更状态
			apply.setId(eaBusinessApply.getParentId());
			eaBusinessApplyMapper.updateById(apply);
		}
		// update-end-Autor: wmf date: 2022-02-08 12:51 for: 把原单据修改为已变更
	}

	@Override
	@Transactional
	public void updateMain(EaBusinessApply eaBusinessApply,List<EaBusinessPerDtl> eaBusinessPerDtlList) {
		eaBusinessApplyMapper.updateById(eaBusinessApply);
		
		//1.先删除子表数据
		eaBusinessPerDtlMapper.deleteByMainId(eaBusinessApply.getId());
		
		//2.子表数据重新插入
		if(eaBusinessPerDtlList!=null && eaBusinessPerDtlList.size()>0) {
			for(EaBusinessPerDtl entity:eaBusinessPerDtlList) {
				//外键设置
				entity.setEaBusinessApplyId(eaBusinessApply.getId());
				eaBusinessPerDtlMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		// update-begin-Autor: wmf date: 2022-02-08 12:51 for: 把原单据修改为已变更
		EaBusinessApply eaBusinessApply = eaBusinessApplyMapper.selectById(id);
		if (eaBusinessApply != null && StringUtils.isNotBlank(eaBusinessApply.getParentId())){
			EaBusinessApply apply = new EaBusinessApply();
			apply.setIfChanged(0); // 改为未变更
			apply.setId(eaBusinessApply.getParentId());
			eaBusinessApplyMapper.updateById(apply);
		}
		// update-end-Autor: wmf date: 2022-02-08 12:51 for: 把原单据修改为已变更

		eaBusinessPerDtlMapper.deleteByMainId(id);
		eaBusinessApplyMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaBusinessPerDtlMapper.deleteByMainId(id.toString());
			eaBusinessApplyMapper.deleteById(id);
		}
	}
	
}
